package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.Converter.UmsMenuConverter;
import com.macro.mall.dao.UmsRoleDao;
import com.macro.mall.mapper.UmsRoleMapper;
import com.macro.mall.mapper.UmsRoleMenuRelationMapper;
import com.macro.mall.mapper.UmsRoleResourceRelationMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.UmsResourceService;
import com.macro.mall.service.UmsRoleService;
import com.macro.mall.vo.UmsMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 后台角色管理Service实现类
 * Created by macro on 2018/9/30.
 */
@Service
public class UmsRoleServiceImpl implements UmsRoleService {
    @Autowired
    private UmsRoleMapper roleMapper;
    @Autowired
    private UmsRoleMenuRelationMapper roleMenuRelationMapper;
    @Autowired
    private UmsRoleResourceRelationMapper roleResourceRelationMapper;
    @Autowired
    private UmsRoleDao roleDao;
    @Autowired
    private UmsResourceService resourceService;
    @Override
    public int create(UmsRole role) {
        role.setCreateTime(new Date());
        role.setAdminCount(0);
        role.setSort(0);
        return roleMapper.insert(role);
    }

    @Override
    public int update(Long id, UmsRole role) {
        role.setId(id);
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public int delete(List<Long> ids) {
        UmsRoleExample example = new UmsRoleExample();
        example.createCriteria().andIdIn(ids);
        int count = roleMapper.deleteByExample(example);
        resourceService.initPathResourceMap();
        return count;
    }

    @Override
    public List<UmsRole> list() {
        return roleMapper.selectByExample(new UmsRoleExample());
    }

    @Override
    public List<UmsRole> list(String keyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        UmsRoleExample example = new UmsRoleExample();
        if (!StringUtils.isEmpty(keyword)) {
            example.createCriteria().andNameLike("%" + keyword + "%");
        }
        return roleMapper.selectByExample(example);
    }

    @Override
    public List<UmsMenuVo> getMenuList(Long adminId) {
        List<UmsMenu> menuList = roleDao.getMenuList(adminId);
        List<UmsMenuVo> umsMenuVoList = new ArrayList<>();

            for (UmsMenu item : menuList) {
                // 判断是否是根节点，就是 parentId，这里是从 0 开始，如果 parentId 为 0 ，则表示根节点
                if (item.getParentId() == 0) {
                    umsMenuVoList.add(UmsMenuConverter.INSTANCES.toStudentHomeVo(item));
                }
            }

            // 根节点添加完就需要添加每个节点的子节点了，这里需要调用 递归方法 getChildren();
            // 遍历根节点数据，为其设置子节点集合
            for (UmsMenuVo item : umsMenuVoList) {
                // 获取子节点数据，传入 当前节点 id 和 所有 list
                List<UmsMenuVo> childList = getChildren(item.getId(), menuList);
                // 将获取到的子节点集合添加到根节点里
                item.setChildren(childList);
            }



        return umsMenuVoList;
    }
    /**
     * 获取子节点数据
     *
     * @param id            父节点 ID
     * @param List<UmsMenu> tests 所有节点集合
     * @return 返回子节点列表
     */
    private List<UmsMenuVo> getChildren(Long id, List<UmsMenu> tests) {
        // 存在子节点数据
        List<UmsMenuVo> childList = new ArrayList<>();
        // 遍历所有节点数据
        for (UmsMenu item : tests) {
            // 如果当前节点 ID 与父节点 ID 一致，表示当前数据是该节点的子节点
            if (Objects.equals(item.getParentId(), id)) {
                // 如果字段太多，超过5个以上，还是建议使用 mapper 方法来转哦，具体如何使用 mapper 将 Entity 转为 DTO 请移步下方链接
                childList.add(UmsMenuConverter.INSTANCES.toStudentHomeVo(item) );
            }
        }
        // 重点来了，递归调用
        for (UmsMenuVo item : childList) {
            // 调用自身方法，依次添加子节点数据
            item.setChildren(getChildren(item.getId(), tests));
        }
        // 如果当前节点无子节点数据添加空数据，递归退出
        if (childList.isEmpty()) {
            return new ArrayList<>();
        }
        // 返回最终的子节点数据
        return childList;
    }


    @Override
    public List<UmsMenu> listMenu(Long roleId) {
        return roleDao.getMenuListByRoleId(roleId);
    }

    @Override
    public List<UmsResource> listResource(Long roleId) {
        return roleDao.getResourceListByRoleId(roleId);
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        UmsRoleMenuRelationExample example=new UmsRoleMenuRelationExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleMenuRelationMapper.deleteByExample(example);
        //批量插入新关系
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            roleMenuRelationMapper.insert(relation);
        }
        return menuIds.size();
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        UmsRoleResourceRelationExample example=new UmsRoleResourceRelationExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleResourceRelationMapper.deleteByExample(example);
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            roleResourceRelationMapper.insert(relation);
        }
        resourceService.initPathResourceMap();
        return resourceIds.size();
    }
}
